﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Repository.PartModels;
using FRL.DebtChased.Website;

namespace FRL.DebtChased.Website.FinanceParts
{

	/// <summary>
	/// Declaration of BaseWebPart class
	/// </summary>
	public class BaseWebPart : WebPart
	{

		/////////////////////////////////////////////////////////////
		// Constants and enumerations

		public enum eMode
		{
			WithoutGrid,
			WithGrid,
		}

		protected const string GridViewId = "gridViewCtrl";
		protected const string ScrollDivId = "scrollDivCtrl";

		/////////////////////////////////////////////////////////////
		// Attributes

		// Single repository instance
		private DebtChasedEntities m_ent = null;

		// Scroll Div
		private Panel m_scrollPanel;
		protected Panel ScrollPanel
		{
			get { return m_scrollPanel; }
		}

		// Grid View Control
		private GridView m_gridView;
		protected GridView GridView
		{
			get { return m_gridView; }
		}

		protected bool IsPartDesignMode
		{
			get { return (this.WebPartManager.DisplayMode != WebPartManager.BrowseDisplayMode); }
		}

		private eMode m_Mode;

		protected BasePartModel m_model = null;

		/////////////////////////////////////////////////////////////
		// Construction

		public BaseWebPart(string strTitle, eMode Mode = eMode.WithGrid)
		{

			// Initialize member variables
			m_Mode = Mode;

			// Initialize the web part
			this.Title = strTitle;
			this.TitleIconImageUrl = "~/Images/PartIcon.jpg";
			if(ContentWidth != 0)
				this.Width = new Unit(ContentWidth, UnitType.Pixel);
			if(ContentHeight != 0)
				this.Height = new Unit(ContentHeight, UnitType.Pixel);

			// Set up event handlers
			this.Load += new EventHandler(BaseWebPart_Load);

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		protected virtual int ContentWidth { get { return 220; } }

		protected virtual int ContentHeight { get { return 220; } }

		protected virtual string GrivViewSkinId { get { return null; } }

		protected virtual void _fillContent(System.Web.UI.HtmlTextWriter w, GridView gridView) { }

		protected virtual void _fillContent(System.Web.UI.HtmlTextWriter w, Panel pnlScrool, GridView gridView) { }

		protected virtual void _processCommand(string strCommandName, int iId) { }

		protected virtual void _loadModel() {}

		protected virtual void _bindGrid(GridView gridView) {}	

		protected virtual void _setFooterCells(GridViewRow row) {}

		protected virtual void _createControls()
		{

			// If this part uses a grid...
			if(m_Mode == eMode.WithGrid)
			{

				// Construct the panel
				m_scrollPanel = new Panel();
				m_scrollPanel.Height = new Unit(this.Height.Value, UnitType.Pixel);
				m_scrollPanel.ScrollBars = System.Web.UI.WebControls.ScrollBars.Vertical;

				// Construct the grid
				m_gridView = new GridView();
				m_gridView.ID = GridViewId;
				m_gridView.SkinID = GrivViewSkinId;
				m_gridView.EnableViewState = false;
				m_gridView.RowCommand += new GridViewCommandEventHandler(GridView_RowCommand);
				m_gridView.RowDataBound += new GridViewRowEventHandler(GridView_RowDataBound);
				m_scrollPanel.Controls.Add(m_gridView);

				// Add to controls
				Controls.Add(m_scrollPanel);

			}
			
		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		void BaseWebPart_Load(object sender, EventArgs e)
		{

			// Create controls here to maintain viewstate
			_createControls();

			// Get my session
			MySession mySession = MySession.BindMySession();

			// If this is a postback, retrieve model from session state
			if (Page.IsPostBack)
				m_model = mySession.FindPartModel(base.UniqueID);

			// If model not loaded, load from the database, and store in session
			if (m_model == null)
			{
				_loadModel();
				mySession.AddPartModel(base.UniqueID, m_model);
			}

			// bind the controls - gridView
			if (m_gridView != null)
				_bindGrid(m_gridView);

		}

		void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
		{

			// Check the grid view object
			GridView gridView = sender as GridView;
#if DEBUG
			Trace.Assert(gridView == m_gridView);
#endif // #if DEBUG

			// Get row index
			int iRow = int.Parse(e.CommandArgument.ToString());

			// Convert argument to id
			int iId = int.Parse(m_gridView.DataKeys[iRow].Value.ToString());

			// Get command and argument
			_processCommand(e.CommandName, iId);

		}

		void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
		{

			// If this is the footer...
			if (
				(m_gridView.ShowFooter)
				&&
				(e.Row.RowType == DataControlRowType.Footer)
			)
				_setFooterCells(e.Row);

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		protected override void RenderContents(System.Web.UI.HtmlTextWriter w)
		{

			// Fill the content
			_fillContent(w, m_scrollPanel, m_gridView);

		}

		public override bool AllowClose
		{
			get
			{
				return true;
			}
			set
			{
				base.AllowClose = value;
			}
		}

		public override bool AllowMinimize
		{
			get
			{
				return false;
			}
			set
			{
				base.AllowMinimize = value;
			}
		}

		/////////////////////////////////////////////////////////////
		// Operations

		protected DebtChasedEntities _getEnt()
		{

			// If repository object not created...
			if (m_ent == null)
				m_ent = new DebtChasedEntities();

			// Return variable
			return m_ent;

		}

	}

}
